package com.song.ssm.ssmvueproject.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.web.servlet.config.annotation.*;

import javax.sql.DataSource;

/**
 * @author TonySong
 * @date 2024/3/30 0030
 * @time 10:35
 */

@EnableWebMvc
@Configuration
@PropertySource(value = "classpath:jdbc.properties")
@ComponentScan("com.song.ssm.ssmvueproject")
@MapperScan("com.song.ssm.ssmvueproject.mapper")
public class MvcConfig implements WebMvcConfigurer {

    @Value("${jdbc.driver}")
     private String driver;

    @Value("${jdbc.url}")
     private String url;

    @Value("${jdbc.username}")
     private String username;

    @Value("${jdbc.password}")
     private String password;

    //数据源配置
    public DataSource  dataSource(){
           DruidDataSource druidDataSource= new DruidDataSource();
           druidDataSource.setDriverClassName(driver);
           druidDataSource.setUrl(url);
           druidDataSource.setUsername(username);
           druidDataSource.setPassword(password);
           return druidDataSource;

    }

//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/**").addResourceLocations("/swagger-ui/**");
//    }

    //mybatis配置
    @Bean
    public SqlSessionFactoryBean  sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean= new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
        return sqlSessionFactoryBean;
    }


    //跨域配置
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }

}
